// 引入Vue
import Vue from "vue";
//引入VueRouter
import VueRouter from "vue-router";

import { getToken, deleteToken } from "@/utils/auth";
import store from "@/store";

import Home from "@/pages/Home";
import Search from "@/pages/Search";
import Category from "@/pages/Category";
import Worth from "@/pages/Worth";
import Cart from "@/pages/Cart";
import Personal from "@/pages/Personal";
import Login from "@/pages/Login";
import Register from "@/pages/Register";
import Order from "@/pages/Order";
import AddressList from "@/pages/Order/components/AddressList";
import AddressEdit from "@/pages/Order/components/AddressEdit";
import Pay from "@/pages/Order/components/Pay";
import PaySuccess from "@/pages/Order/components/PaySuccess";
import Detail from "@/pages/Detail";
import Channel from "@/pages/Channel";
import SubCateList from "@/pages/Category/components/SubCateList";

//应用VueRouter
Vue.use(VueRouter);

//第一步：将VueRouter原型对象上的push缓存一份
const originPush = VueRouter.prototype.push;
//第一步：将VueRouter原型对象上的replace缓存一份
const originReplace = VueRouter.prototype.replace;

//第二步：增强push
VueRouter.prototype.push = function (location, okCallback, errCallback) {
  //如果程序员调用this.$router.push的时候，只传了跳转的位置
  if (okCallback === undefined && errCallback === undefined) {
    return originPush.call(this, location).catch(() => {});
  } else {
    return originPush.call(this, location, okCallback, errCallback);
  }
};
//第二步：增强replace
VueRouter.prototype.replace = function (location, okCallback, errCallback) {
  //如果程序员调用this.$router.push的时候，只传了跳转的位置
  if (okCallback === undefined && errCallback === undefined) {
    return originReplace.call(this, location).catch(() => {});
  } else {
    return originReplace.call(this, location, okCallback, errCallback);
  }
};

// 创建一个路由器
const router = new VueRouter({
  mode: "history",
  routes: [
    {
      name: "home",
      path: "/home",
      component: Home,
      meta: { showFooter: true, showTab: true },
    },
    {
      name: "search",
      path: "/search",
      component: Search,
      meta: { showFooter: false },
    },
    {
      name: "category",
      path: "/category",
      component: Category,
      meta: { showFooter: true },
    },
    {
      name: "worth",
      path: "/worth",
      component: Worth,
      meta: { showNavBar: true, showFooter: true },
    },
    {
      name: "cart",
      path: "/cart",
      component: Cart,
      meta: { showFooter: true },
    },
    {
      name: "personal",
      path: "/personal",
      component: Personal,
      meta: { showFooter: true },
    },
    {
      name: "register",
      path: "/register",
      component: Register,
      meta: { showFooter: false },
    },
    {
      name: "login",
      path: "/login",
      component: Login,
      meta: { showFooter: false, showNavBar: true },
    },
    {
      name: "order",
      path: "/order",
      component: Order,
      meta: { showNavBar: true, showTabbar: false },
    },
    {
      name: "addressList",
      path: "/addressList",
      component: AddressList,
      meta: { showNavBar: true, showTabbar: false },
    },
    {
      name: "addressEdit",
      path: "/addressEdit",
      component: AddressEdit,
      meta: { showNavBar: true, showTabbar: false },
    },
    {
      name: "pay",
      path: "/pay",
      component: Pay,
      meta: { showNavBar: true, showTabbar: false },
    },
    {
      name: "paysuccess",
      path: "/paysuccess",
      component: PaySuccess,
      meta: { showNavBar: true, showTabbar: false },
    },
    {
      name: "detail",
      path: "/detail",
      component: Detail,
      meta: { showNavBar: true, showTabbar: false },
    },
    {
      name: "channel",
      path: "/channel",
      component: Channel,
      meta: { showFooter: true, showTab: true },
    },
    {
      name: "subcateList",
      path: "/subcateList",
      component: SubCateList,
      meta: { showFooter: false },
    },
    {
      path: "/",
      redirect: "/home",
    },
  ],
});


// 没有登录不准去的页面
// const authPath = ['/personal']
// 路由守卫

// 路由守卫
const authPath = ["/personal"];

router.beforeEach(async (to, from, next) => {
  //尝试从local中获取token
  const token = getToken();
  //尝试从vuex中获取userInfo
  const userInfo = store.state.user.userInfo;
  //判断是否登录
  if (token) {
    //登录了
    // console.log('你有token，肯定登录了，我要进一步看看vuex中是否有你的信息')
    //判断是否有用户信息
    if (userInfo.id) {
      //有用户信息
      console.log("你也有个人信息，要啥有啥，直接放行！");
      next(); //无条件放行
    } else {
      //无用户信息
      // console.log('你没有个人信息，我携带者token帮你获取一下个人信息')
      try {
        //走流程去获取用户信息
        await store.dispatch("getUserInfo");
        //获取成功了
        // console.log('你的个人信息获取成功了，且我已经存入vuex了，直接放行！')
        next(); //直接放行
      } catch (error) {
        //获取失败了
        // console.log('你的个人信息获取失败了，可能：token过期了、别人顶号了，我要干掉曾经的token，新登录了解一下？')
        //清空已经过期的token
        deleteToken();
        //打回登录
        next("/login");
      }
    }
  } else {
    //你肯定没登录
    if (authPath.indexOf(to.path) !== -1) {
      next("/login");
    } else {
      next();
    }
  }
});

// 暴露路由
export default router;
